home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / whisperBack / utils.py < prev    next >
Encoding:
Python Source  |  2012-12-13  |  4.9 KB  |  170 lines

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. ########################################################################
  5. # WhisperBack - Send feedback in an encrypted mail
  6. # Copyright (C) 2009-2012 Tails developers <amnesia.org>
  7. #
  8. # This file is part of WhisperBack
  9. #
  10. # WhisperBack is  free software; you can redistribute  it and/or modify
  11. # it under the  terms of the GNU General Public  License as published by
  12. # the Free Software Foundation; either  version 3 of the License, or (at
  13. # your option) any later version.
  14. # This program  is distributed in the  hope that it will  be useful, but
  15. # WITHOUT   ANY  WARRANTY;   without  even   the  implied   warranty  of
  16. # MERCHANTABILITY  or FITNESS  FOR A  PARTICULAR PURPOSE.   See  the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. ########################################################################
  22.  
  23. """various WhisperBack utility functions
  24.  
  25. """
  26.  
  27. import os
  28. import re
  29. import urlparse
  30. import locale
  31.  
  32. # Ugly pathes finder utilities
  33.  
  34. def guess_prefix ():
  35.     """Tries to guess the prefix
  36.  
  37.     @return The guessed prefix"""
  38.  
  39.     # XXX: hardcoded path !
  40.     if os.path.exists ("/usr/local/share/whisperback"):
  41.         return "/usr/local"
  42.     elif os.path.exists ("/usr/share/whisperback"):
  43.         return "/usr"
  44.     else:
  45.         return None
  46.  
  47. def get_sharedir ():
  48.     """Tries to guess the shared data directiry
  49.  
  50.     @return The guessed shared data directiry"""
  51.  
  52.     if guess_prefix():
  53.         return os.path.join (guess_prefix(), "share")
  54.     else:
  55.         return "data"
  56.  
  57. def get_datadir ():
  58.     """Tries to guess the datadir
  59.  
  60.     @return The guessed datadir"""
  61.     if guess_prefix():
  62.         return os.path.join (get_sharedir(), "whisperback")
  63.     else:
  64.         return "data"
  65.  
  66. def get_pixmapdir ():
  67.     """Tries to guess the pixmapdir
  68.  
  69.     @return The guessed pixmapdir"""
  70.  
  71.     if guess_prefix():
  72.         return os.path.join (get_sharedir(), "pixmaps")
  73.     else:
  74.         return "data"
  75.  
  76. # Input validation fuctions
  77.  
  78. def is_valid_link(candidate):
  79.     """Check if candidate seems to be a internet link
  80.  
  81.     @param candidate the URL to be checked
  82.  
  83.     @returns true if candidate is an URL with:
  84.     - an hostname of the form domain.tld
  85.     - a scheme http(s) or ftp(S)
  86.     """
  87.     parseresult = urlparse.urlparse(candidate)
  88.     #pylint: disable=E1101
  89.     if (re.search(r'^(ht|f)tp(s)?$', parseresult.scheme) and
  90.         re.search(r'^(\w{1,}\.){1,}\w{1,}$', parseresult.hostname)):
  91.         return True
  92.     else:
  93.         return False
  94.  
  95. def is_valid_pgp_block(candidate):
  96.     """Check if candidate seems to be a PGP public key block
  97.  
  98.     @param    candidate the string to be checked
  99.  
  100.     @returns  true if candidate starts with `-----BEGIN PGP PUBLIC KEY BLOCK----`
  101.               and ends with `-----END PGP PUBLIC KEY BLOCK-----`
  102.     """
  103.     #pylint: disable=C0301
  104.     if re.search(r"-----BEGIN PGP PUBLIC KEY BLOCK-----\n(?:.*\n)+-----END PGP PUBLIC KEY BLOCK-----",
  105.             candidate):
  106.         return True
  107.     else:
  108.         return False
  109.  
  110. def is_valid_pgp_id(candidate):
  111.     """Check if candidate looks like a pgp key ID
  112.  
  113.     @param    candidate the string to be checked
  114.  
  115.     @returns  true if candidate is either an 8 or 16 digit hex number or a 40
  116.               digit hex fingerprint
  117.     """
  118.     #pylint: disable=C0301
  119.     if re.search(r"(?:^(?:0x)?(?:[0-9a-fA-F]{8}){1,2}$)|(?:^(?:[0-9f-zA-F]{4} {0,2}){10}$)",
  120.             candidate):
  121.         return True
  122.     else:
  123.         return False
  124.  
  125. def is_valid_email(candidate):
  126.     """Check if candidate looks like an email address
  127.  
  128.     @param    candidate the string to be checked
  129.  
  130.     @returns  true if candidate is in the form test@example.com
  131.     """
  132.     if re.search(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", candidate):
  133.         return True
  134.     else:
  135.         return False
  136.  
  137. def sanitize_hardware_info(log_string):
  138.     """Sanitize hardware-identifying info from a string
  139.  
  140.     Removes strings:
  141.     
  142.     - labeled as serial numbers and UUID;
  143.     - looking like IPs or MAC addresses.
  144.  
  145.     @param  log_string  the string to be sanitized
  146.  
  147.     @returns a sanitized version of log_string
  148.     """
  149.     # XXX: must be updated once IPv6 is enabled
  150.  
  151.     # Serial Numbers
  152.     log_string = re.sub(r'((Serial Number:?[\s]+|SerialNo=|iSerial[\s]+[\d]+\s+|SerialNumber:|Serial#:)[\s]+)[^\s].+',
  153.                         r'\1[SN REMOVED]',
  154.                         log_string)
  155.     # UUIDs
  156.     log_string = re.sub(r'(UUID:[\s]+)[^\s].+',
  157.                         r'\1[UUID REMOVED]',
  158.                         log_string)
  159.  
  160.     # IPs
  161.     log_string = re.sub(r'\<([\d]{1,3}\.){3}[\d]{1,3}\>',
  162.                         r'[IP REMOVED]',
  163.                         log_string)
  164.     # MAC addresses
  165.     log_string = re.sub(r'\<([0-9a-fA-F]{2}:){5,}[0-9a-fA-F]{2}\>',
  166.                         r'[MAC REMOVED]',
  167.                         log_string)
  168.     return log_string
  169.